<?php
namespace PsiFramework\Classes\Util\Object;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 * The TextCollection class is used to manage Text Collections
 *
 * @package Util.Object
 */
class TextCollection extends Collection
{

    public function __construct(/*CollectionElementText[]*/ $elements=array())
    {
        parent::__construct($elements);
    }
    
    /**
     * 
     * @param string $text
     * 
     * @return bool
     */
    public function contains(/*string*/ $text)
    {
        foreach ($this->_elements as $element) {
            if ($element->getValue() == $text) {
                return true;
            }
        }
        return false;
    }
    
    public function size()
    {
        return count($this->_elements);
    }
    
    public function getText(/*int*/ $index)
    {
        if ($index < $this->size()) {
            return $this->_elements[$index]->getValue();
    	} else {
    	    return null;
    	}
    }
    
    public function first()
    {
        return $this->getText(0);
    }
    
    public function rand()
    {
        $index = rand(0, $this->size());
        return $this->getText($index);
    }
    
    public function unserialize(/*string*/ $text)
    {
        $jsonObject = new Json();
        $jsonObject->unserialize($text);
        
        $object = $jsonObject->getObject();
        foreach ($object as $jsonElement) {
            $element = new CollectionElementText();
            $element->unserialize($jsonElement);
            $this->_elements[] = $element;
        }
    }
    
    public function toString()
    {
        return $this->serialize();
    }
}
